<?php

class PaymentPartConverter {
	public static function convert($isPaid, $tillDate, $part, $price) {
		$result = new PaymentPart ( );
		$result->setPaid ( VariableUtils::getBoolean ( $isPaid ) );
		$result->setTillDate ( @date ( "Y-m-d", @strtotime ( $tillDate ) ) );
		$result->setPart ( $part );
		$result->setValue ( $price );
		return $result;
	}
}

class PaymentGetCommand implements Command{
	private $usid;
	public function __construct($usid){
		$this->usid= $usid;
	}
	public function doExecute($connection){
		if(!VariableUtils::isNumber($this->usid)){
			return false;
		}


		$result = new Payment();
		$query = "select pt." 
		. BoaConstans::PAYMENT_COLUMN_IS_PAID  . ",  pt."
		. BoaConstans::PAYMENT_COLUMN_PAID_TILL_DATE . ", pt."
		. BoaConstans::PAYMENT_COLUMN_PART . ", ust." . 
		BoaConstans::USER_SERVICE_COLUMN_PRICE . 
		" from " . BoaConstans::PAYMENT_TABLE ." pt join " . BoaConstans::USER_SERVICE_TABLE . " ust on pt." 
		. BoaConstans::PAYMENT_COLUMN_USID . "=ust." . BoaConstans::USER_SERVICE_COLUMN_ID . " where pt."
		. BoaConstans::PAYMENT_COLUMN_USID . "=?";

		$stmt = $connection->prepare($query);
		$stmt->bind_param("i", $this->usid);
		$stmt->execute();
		
		$isPaid = null;
		$tillDate = null;
		$part = null;
		$price = null;
		$stmt->bind_result ( $isPaid, $tillDate, $part, $price );
		while ( $stmt->fetch () ) {
			$result->addPart(PaymentPartConverter::convert( $isPaid, $tillDate, $part, $price));
		}
		return $result;
	}
}

class PaymentEnableWarnCommand implements Command {
	private $usid;
	public function __construct($usid){
		$this->usid = $usid;
	}
	public function doExecute($connection){
		$query = "update " . BoaConstans::USER_SERVICE_TABLE . " set " . 
		BoaConstans::USER_SERVICE_COLUMN_WARN . "=? where " . 
		BoaConstans::USER_SERVICE_COLUMN_ID . "=?";
		
		$warn = true;
		$stmt = $connection->prepare($query);
		$stmt->bind_param("ii", $warn, $this->usid);
		$stmt->execute();
		$stmt->close();
		return true;
	}
}
class PaymentDisableWarnCommand implements Command{
	private $usid;
	public function __construct($usid){
		$this->usid = $usid;
	}
	public function doExecute($connection){
		$query = "update " . BoaConstans::USER_SERVICE_TABLE . " set " . 
		BoaConstans::USER_SERVICE_COLUMN_WARN . "=? where " . 
		BoaConstans::USER_SERVICE_COLUMN_ID . "=?";
		$warn = false;
		$stmt = $connection->prepare($query);
		$stmt->bind_param("ii", $warn, $this->usid);
		$stmt->execute();
		$stmt->close();
		return true;
	}
}

class PaymentHasWarnCommand implements Command{
	private $usid;
	public function __construct($usid){
		$this->usid = $usid;
	}
	public function doExecute($connection){
		$query = "select " . BoaConstans::USER_SERVICE_COLUMN_WARN . " from  " . BoaConstans::USER_SERVICE_TABLE .  
		" where " . BoaConstans::USER_SERVICE_COLUMN_ID . "=?";
		$stmt = $connection->prepare($query);
		$stmt->bind_param("i", $this->usid);
		$stmt->execute();
		$warn = null;
		$stmt->bind_result($warn);
		$stmt->fetch();
		$stmt->close();
		return VariableUtils::getBoolean($warn);
	}
}

?>